/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c) 2002, GeoVISTA Center
 * Licensed under Simplified BSD License
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * @original author: jin Chen 
 * @author: jin Chen 
 * @date: Nov 17, 2008$
 * @version: 1.0
 */
package edu.psu.geovista.app.vit.pub.action;



import epoviz.trex.common.swing.components.table.configtable.SelectItemTablePanel;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Set;

public class CalculateReliabilityValuesPane extends javax.swing.JPanel {
 JFileChooser filechooser;
 private File root;//root dir for FileChooser
 SelectItemTablePanel tablep;
    public  static final String SESSION_IDS = "Session Ids";

    /** Creates new form GetClusterReliabilityPane */
 public CalculateReliabilityValuesPane(String[] srcdataNames) {

     initComponents();
     ComboBoxModel model=new DefaultComboBoxModel(srcdataNames);
     this.srcdataBox.setModel(model);

     tablep=new SelectItemTablePanel();
     this.tablePane.setLayout(new BorderLayout());
     tablePane.add(tablep);
 }

    public void setSaTScanSessionIds(Object[] ids){
     this.tablep.setData(SESSION_IDS,ids);
 }
 public Set getSelectedSessionIds(){
     return this.tablep.getSelectedItems(SESSION_IDS);
 }
 /*******************************************************************************************************
  *                service
  *******************************************************************************************************/
 public double getSignificantValue(){
     String  pvalue = (String) this.sigCombox.getSelectedItem();
     return Double.parseDouble(pvalue);
 }
 public String getSourceDataName(){
     return (String) this.srcdataBox.getSelectedItem();
 }
 public String getVarName(){
     return this.varnFd.getText() ;
 }
    public String getDataname(){
       return datanameFd.getText().trim();
    }
   /* public JTextField getDatanameFd() {
        return datanameFd;
    }*/
 public String getDataPath(){
     return this.datapathFd.getText();
 }
 private void setDataPath(){
     // Add your handling code here:
     String extension="csv";

     filechooser = new JFileChooser();
     filechooser.setFileFilter(new FileNameExtensionFilter(extension,extension));
     filechooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
     //fc.set
     if(getRoot()!=null&&getRoot().isDirectory() ){
         filechooser.setCurrentDirectory(getRoot());
     }
     else {
         filechooser.setCurrentDirectory(new File("c:/"));
     }
     filechooser.setDialogTitle("Please choose a file");
     //fc.setDialogType(JFileChooser.OPEN_DIALOG );
     int state= 0;

     state=filechooser.showSaveDialog(this);


     if(state==JFileChooser.APPROVE_OPTION ){
         File selected = filechooser.getSelectedFile() ;
             String dirname=null ;
             try {
                 dirname = selected.getCanonicalPath();

             } catch (IOException e) {
                 e.printStackTrace();
                 //???exception
             }
         String myext = "." + extension;
         dirname=dirname.replaceAll(myext,"");
         String filename = datanameFd.getText();
         String fullname = dirname + File.separator +filename+myext;
         this.datapathFd.setText(fullname);
     }
 }

    public File getRoot() {
        return root;
    }

    public void setRoot(File root) {
        this.root = root;
    }

    /** This method is called from within the constructor to
  * initialize the form.
  * WARNING: Do NOT modify this code. The content of this method is
  * always regenerated by the Form Editor.
  */
 // <editor-fold defaultstate="collapsed" desc="Generated Code">
 private void initComponents() {

     jPanel2 = new JPanel();
     jPanel3 = new JPanel();
     jPanel1 = new JPanel();
     sigCombox = new JComboBox();
     sigfLb = new JLabel();
     varnLb = new JLabel();
     varnFd = new JTextField();
     datapathBtn = new JButton();
     datapathFd = new JTextField();
     srcDataLabel = new JLabel();
     srcdataBox = new JComboBox();
     jLabel1 = new JLabel();
     datanameFd = new JTextField();
     tablePane = new JPanel();

     GroupLayout jPanel2Layout = new GroupLayout(jPanel2);
     jPanel2.setLayout(jPanel2Layout);
     jPanel2Layout.setHorizontalGroup(
         jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addGap(0, 100, Short.MAX_VALUE)
     );
     jPanel2Layout.setVerticalGroup(
         jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addGap(0, 100, Short.MAX_VALUE)
     );

     GroupLayout jPanel3Layout = new GroupLayout(jPanel3);
     jPanel3.setLayout(jPanel3Layout);
     jPanel3Layout.setHorizontalGroup(
         jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addGap(0, 100, Short.MAX_VALUE)
     );
     jPanel3Layout.setVerticalGroup(
         jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addGap(0, 100, Short.MAX_VALUE)
     );

     jPanel1.setBorder(BorderFactory.createTitledBorder(null, "Calculate reliablity for valid clusters", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new Font("Tahoma", 0, 14)));

     sigCombox.setModel(new DefaultComboBoxModel(new String[] { "0.05", "0.01" }));

     sigfLb.setText("Significant value for vallid clusters");
     sigfLb.setToolTipText("A cluster will be counted as valid if it has p-value less than the Significant value");

     varnLb.setText("Name of the reliability attribute");
     varnLb.setToolTipText("Please set the attribute(column) name for the variable that measures the percentage a data item is considered as being significant.");

     varnFd.setText("Reliability");
     varnFd.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
             varnFdActionPerformed(evt);
         }
     });

     datapathBtn.setText("Output Folder (Optional)");
     datapathBtn.setToolTipText("Set Output folder where the reliability data will be stored.");
     datapathBtn.setActionCommand("Output Data. The cluster's reliability data will be added as a new data.");
     datapathBtn.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
             datapathBtnActionPerformed(evt);
         }
     });

     datapathFd.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
             datapathFdActionPerformed(evt);
         }
     });

     srcDataLabel.setText("Source Data");
     srcDataLabel.setToolTipText("The output data will need data ids(e.g. FIPS), which are obtained from the source data");

     srcdataBox.setModel(new DefaultComboBoxModel(new String[] { "data1", "data2", "data3", "data4" }));

     jLabel1.setText("Data name");
     jLabel1.setToolTipText("The reliability data will be added as a new data with the specified name");

     datanameFd.setText("Reliability");
     datanameFd.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
             datanameFdActionPerformed(evt);
         }
     });

     GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
     jPanel1.setLayout(jPanel1Layout);
     jPanel1Layout.setHorizontalGroup(
         jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addGroup(jPanel1Layout.createSequentialGroup()
             .addGap(10, 10, 10)
             .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                 .addComponent(srcDataLabel)
                 .addComponent(sigfLb)
                 .addComponent(varnLb)
                 .addComponent(datapathBtn)
                 .addComponent(jLabel1))
             .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
             .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                 .addComponent(sigCombox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                 .addComponent(srcdataBox, 0, 325, Short.MAX_VALUE)
                 .addComponent(datanameFd, GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                 .addComponent(datapathFd, GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                 .addComponent(varnFd, GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE))
             .addContainerGap())
     );
     jPanel1Layout.setVerticalGroup(
         jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addGroup(jPanel1Layout.createSequentialGroup()
             .addContainerGap()
             .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                 .addComponent(sigfLb)
                 .addComponent(sigCombox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
             .addGap(18, 18, 18)
             .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                 .addComponent(srcDataLabel)
                 .addComponent(srcdataBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
             .addGap(14, 14, 14)
             .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                 .addComponent(varnLb)
                 .addComponent(varnFd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
             .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
             .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                 .addComponent(datanameFd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                 .addComponent(jLabel1))
             .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
             .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                 .addComponent(datapathBtn)
                 .addComponent(datapathFd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
             .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
     );

     tablePane.setBorder(BorderFactory.createTitledBorder("Select SaTScan Sessions for Calculation"));

     GroupLayout tablePaneLayout = new GroupLayout(tablePane);
     tablePane.setLayout(tablePaneLayout);
     tablePaneLayout.setHorizontalGroup(
         tablePaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addGap(0, 513, Short.MAX_VALUE)
     );
     tablePaneLayout.setVerticalGroup(
         tablePaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addGap(0, 175, Short.MAX_VALUE)
     );

     GroupLayout layout = new GroupLayout(this);
     this.setLayout(layout);
     layout.setHorizontalGroup(
         layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
             .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                 .addComponent(tablePane, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                 .addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
             .addContainerGap())
     );
     layout.setVerticalGroup(
         layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
             .addContainerGap()
             .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
             .addGap(18, 18, 18)
             .addComponent(tablePane, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
             .addContainerGap())
     );
 }// </editor-fold>

 private void varnFdActionPerformed(java.awt.event.ActionEvent evt) {
     // TODO add your handling code here:
}

 private void datapathBtnActionPerformed(java.awt.event.ActionEvent evt) {
    setDataPath();
}

 private void datapathFdActionPerformed(java.awt.event.ActionEvent evt) {
     // TODO add your handling code here:
}
    private void datanameFdActionPerformed(java.awt.event.ActionEvent evt) {
     // TODO add your handling code here:
}


 // Variables declaration - do not modify
 private JTextField datanameFd;
 private JButton datapathBtn;
 private JTextField datapathFd;
 private JLabel jLabel1;
 private JPanel jPanel1;
 private JPanel jPanel2;
 private JPanel jPanel3;
 private JComboBox sigCombox;
 private JLabel sigfLb;
 private JLabel srcDataLabel;
 private JComboBox srcdataBox;
 private JPanel tablePane;
 private JTextField varnFd;
 private JLabel varnLb;
 // End of variables declaration


}//inter class